package com.codeplateform.controller;

import com.codeplateform.common.ResultModel;
import com.codeplateform.dao.entity.CodeCondition;
import com.codeplateform.dao.entity.CodeFunction;
import com.codeplateform.service.CodeFunctionService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/codeFunction")
public class CodeFunctionController {

    @Autowired
    CodeFunctionService codeFunctionService;



    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public ResultModel upload(@RequestBody CodeFunction codeFunction){
        log.info("上传功能名称为{}",codeFunction.getCodeName());

        codeFunction.setCreatedDate(new Date());
        codeFunction.setRemark(0);

        int insert = codeFunctionService.insert(codeFunction);

        if (insert==1){
            return ResultModel.success("提交成功");
        }else {

            return ResultModel.error(500,"提交异常");
        }

    }

    @RequestMapping(value = "/getCodeList",method = RequestMethod.POST)
    public ResultModel getCodeList(@RequestBody CodeFunction codeFunction){
      /*  if (null==codeFunction){
            return
        }*/
        log.info("获取列表方法入参位{}",codeFunction);
        List<CodeFunction> codeFunctions = codeFunctionService.selectCode(codeFunction);
        if (null != codeFunctions && codeFunctions.size() > 0){
            return ResultModel.success(codeFunctions);
        }
        return ResultModel.error(500,"未查询到符合条件");

    }

    @RequestMapping(value = "/getCodeByLimte",method = RequestMethod.POST)
    public ResultModel getCodeByLimit(@RequestBody CodeCondition codeCondition){
        log.info(codeCondition+"==============");

        if (null!=(codeCondition)){
            CodeFunction codeFunction=new CodeFunction();

            codeFunction.setCodeName(codeCondition.getCodeName());
            List<CodeFunction> codeFunctions = codeFunctionService.selelctByLimite(codeFunction);

            return ResultModel.success("查询成功",codeFunctions);
        }else {
            return ResultModel.error(500,"请求参数为空");
        }

    }
}
